﻿@using System.Linq.Expressions;
@using BlazorComponent;
@using Masa.Blazor.Presets;
@page "/gatewayconfig/channel"
@using Microsoft.AspNetCore.Authorization;
@using System.IO.Ports
@using ThingsGateway.Admin.Application;
@using ThingsGateway.Admin.Blazor
@using ThingsGateway.Core.Extension
@using ThingsGateway.Foundation
@using ThingsGateway.Gateway.Application

@namespace ThingsGateway.Gateway.Blazor
@attribute [Authorize]
@inject UserResoures UserResoures
@attribute [RolePermission]
@inherits BaseComponentBase


<AppDataTable @ref="_datatable" TItem="Channel" SearchItem="ChannelPageInput" AddItem="ChannelAddInput" EditItem="ChannelEditInput"
              SearchModel="@_search" IsShowSearchKey
               QueryCallAsync="QueryCallAsync" AddCallAsync="AddCallAsync"
               EditCallAsync="EditCallAsync" DeleteCallAsync="DeleteCallAsync"
               IsMenuOperTemplate=false AddWidth="1600" EditWidth="1600"
                IsShowDetailButton
                IsShowQueryButton
               IsShowAddButton=@UserResoures.IsHasButtonWithRole("GatewayChannelAdd")
               IsShowDeleteButton=@UserResoures.IsHasButtonWithRole("GatewayChannelDelete")
               IsShowEditButton=@UserResoures.IsHasButtonWithRole("GatewayChannelEdit")>

     <SearchTemplate>

         <MTextField Dense Style="max-width:200px;" HideDetails=@("auto") Class="my-1 mx-2 " Outlined
                     @bind-Value="context.Name" Label=@AppService.I18n.T(context.Description(x => x.Name)) />
         <MSelect Class="my-1 mx-2 " Items=@(typeof(ChannelTypeEnum).GetEnumList()) Style="max-width:300px;"
                  @bind-Value=@context.ChannelType Label=@(context.Description(x => x.ChannelType))
                  MenuProps="@(props => { props.Auto = true; props.OffsetY = true; })"
                  ItemText=@(u =>u.Description) Clearable
                  ItemValue=@(u =>(ChannelTypeEnum)u.Value)
                  Dense HideDetails=@("auto")
                  Outlined></MSelect>
     </SearchTemplate>

     <AddTemplate>
         @GetRenderFragment(context)

    </AddTemplate>

    <EditTemplate>
        @GetRenderFragment(context)
    </EditTemplate>

    <ItemColTemplate>
        @switch (context.Header.Value)
        {
            case nameof(context.Item.Enable):
                <EnableChip Value="context.Item.Enable">
                </EnableChip>
                break;
            default:
                @if (context.Header.CellClass?.Contains("text-truncate") == true)
                {
                    <span title=@context.Value>
                        @context.Value
                    </span>
                }
                else
                {
                    @context.Value
                }
                break;
        }

    </ItemColTemplate>
    <OtherToolbarTemplate>

        <MMenu OffsetY Context="menu">
             <ActivatorContent>
                 <MButton @attributes="@menu.Attrs" Color="primary"
                          Class="my-1  mx-2 ">
                     @AppService.I18n.T("导出")
                    <AppChevronDown></AppChevronDown>
                </MButton>
            </ActivatorContent>
            <ChildContent>
                <MList>
                    <MListItem OnClick="()=>DownExportAsync(true)">@AppService.I18n.T("导出全部")</MListItem>
                        <MListItem OnClick="()=>DownExportAsync(false)">@AppService.I18n.T("导出搜索项")</MListItem>
                    </MList>
                </ChildContent>
            </MMenu>

            <MButton Disabled=@(!UserResoures.IsHasButtonWithRole("GatewayChannelAdd")) Class="mx-2 my-1" OnClick=@(()=>CopyClickAsync(context)) Color="primary">@AppService.I18n.T("复制")</MButton>
            <MButton Disabled=@(!UserResoures.IsHasButtonWithRole("GatewayChannelImport")) Class=" mx-2 my-1 " OnClick=@(ImportClickAsync) Color="primary">
                @AppService.I18n.T("导入")
            </MButton>
            <MButton Disabled=@(!UserResoures.IsHasButtonWithRole("GatewayChannelClear"))  Class="mx-2 my-1" OnClick=@(()=>DeleteAllAsync()) Color="primary">@AppService.I18n.T("清空")</MButton>
    </OtherToolbarTemplate>


</AppDataTable>



@code {

    RenderFragment GetRenderFragment(ChannelAddInput context)
    {
        RenderFragment renderFragment =
    @<MCard Style="min-height:500px;" Flat Elevation="0" Class="ma-5">
        <MContainer>
            <MRow Justify="JustifyTypes.Start" Align="AlignTypes.Center">

                <MCol Md=12 Cols=12>
                    <MSubheader Style="max-height:24px" Class="font-weight-black text-subtitle-1">
                        @AppService.I18n.T("基础属性")
                    </MSubheader>
                </MCol>
                <MCol Md=3 Cols=12>
                    <MTextField Dense Style="max-width:300px" Outlined HideDetails="@("auto")" Color="primary" @bind-Value=@context.Name Label="@(context.Description(x => x.Name))" />
                </MCol>
                <MCol Md=3 Cols=12>
                    <MSelect Items=@(typeof(ChannelTypeEnum).GetEnumList()) Style="max-width:300px;"
                             @bind-Value=@context.ChannelType Label=@(context.Description(x => x.ChannelType))
                             MenuProps="@(props => { props.Auto = true; props.OffsetY = true; })"
                             ItemText=@(u =>u.Description)
                             ItemValue=@(u =>(ChannelTypeEnum)u.Value)
                             Dense HideDetails=@("auto")
                             Outlined></MSelect>
                </MCol>
                <MCol Md=3 Cols=12>
                    <MRadioGroup @bind-Value=@context.Enable Label=@(context.Description(x => x.Enable))
                                 Style="max-width:300px"
                                 Row Dense HideDetails="@("auto")">
                        <MRadio Label=@AppService.I18n.T("是") Value=true></MRadio>
                        <MRadio Label=@AppService.I18n.T("否") Value=false></MRadio>
                    </MRadioGroup>
                </MCol>
                <MCol Md=3 Cols=12>
                    <MRadioGroup @bind-Value=@context.LogEnable Label=@(context.Description(x => x.LogEnable))
                                 Style="max-width:300px"
                                 Row Dense HideDetails="@("auto")">
                        <MRadio Label=@AppService.I18n.T("是") Value=true></MRadio>
                        <MRadio Label=@AppService.I18n.T("否") Value=false></MRadio>
                    </MRadioGroup>
                </MCol>

            </MRow>
            @{

        var isTcpClient=context.ChannelType==ChannelTypeEnum.TcpClient;
        var isTcpService=context.ChannelType==ChannelTypeEnum.TcpService;
        var isSerial=context.ChannelType==ChannelTypeEnum.SerialPortClient;
        var isUdp=context.ChannelType==ChannelTypeEnum.UdpSession;

            }
            <MRow Class="mt-5" Justify="JustifyTypes.Start" Align="AlignTypes.Center">
                <MCol Md=12 Cols=12>
                    <MSubheader Style="max-height:24px" Class="font-weight-black text-subtitle-1">
                        @AppService.I18n.T("通道属性")
                    </MSubheader>
                </MCol>


                <MCol Md=3 Cols=12>
                    <MTextField Style="max-width:300px" Disabled=@(!isTcpClient&&!isUdp) Dense Outlined HideDetails="@("auto")"
                                Label=@AppService.I18n.T("RemoteUrl") @bind-Value=@context.RemoteUrl />
                </MCol>
                <MCol Md=3 Cols=12>
                    <MTextField Style="max-width:300px" Disabled=@(!isTcpClient&&!isTcpService&&!isUdp) Dense Outlined HideDetails="@("auto")"
                                Label=@AppService.I18n.T("BindUrl") @bind-Value=@context.BindUrl />
                </MCol>
            </MRow>
            <MRow Class="mt-5" Justify="JustifyTypes.Start" Align="AlignTypes.Center">

                <MCol Md=3 Cols=12>
                    <MTextField Style="max-width:300px" Disabled=!isSerial Dense Outlined HideDetails="@("auto")"
                                Label=@AppService.I18n.T("PortName") @bind-Value=@context.PortName />
                </MCol>
                <MCol Md=3 Cols=12>
                    <MTextField Type="number" Style="max-width:300px" Disabled=!isSerial Dense Outlined HideDetails="@("auto")"
                                Label=@AppService.I18n.T("BaudRate") @bind-Value=@context.BaudRate />
                </MCol>

                <MCol Md=3 Cols=12>
                    <MTextField Style="max-width:300px" Disabled=!isSerial Dense Outlined HideDetails="@("auto")"
                                Type="number" Label=@AppService.I18n.T("DataBits") @bind-Value=@context.DataBits />
                </MCol>

                <MCol Md=3 Cols=12>
                    <MSelect Style="max-width:300px" Disabled=!isSerial Dense Outlined HideDetails="@("auto")"
                             Label=@AppService.I18n.T("Parity") @bind-Value=@context.Parity
                             Items=@(typeof(Parity).GetEnumList())
                             MenuProps="@(props => { props.Auto = true; props.OffsetY = true; })"
                             ItemText=@(u =>u.Description)
                             ItemValue=@(u =>(Parity)u.Value) Height="30">
                    </MSelect>
                </MCol>

                <MCol Md=3 Cols=12>
                    <MSelect Style="max-width:300px" Disabled=!isSerial Dense Outlined HideDetails="@("auto")"
                             Label=@AppService.I18n.T("StopBits") @bind-Value=@context.StopBits
                             Items=@(typeof(StopBits).GetEnumList())
                             MenuProps="@(props => { props.Auto = true; props.OffsetY = true; })"
                             ItemText=@(u =>u.Description)
                             ItemValue=@(u =>(StopBits)u.Value) Height="30">
                    </MSelect>
                </MCol>

                <MCol Md=3 Cols=12>
                    <MSelect Class="" Items=@(new List<bool>(){true,false})
                             Label=@AppService.I18n.T("DtrEnable") @bind-Value=@context.DtrEnable
                             Clearable Style="max-width:300px" Disabled=!isSerial
                             MenuProps="@(props => { props.Auto = true; props.OffsetY = true; })"
                             ItemText=@((u) =>u.ToString()) 
                             ItemValue=@(u =>u)
                             Dense HideDetails=@("auto")
                             Outlined></MSelect>

                </MCol>
                <MCol Md=3 Cols=12>
                    <MSelect Class="" Items=@(new List<bool>(){true,false})
                             Label=@AppService.I18n.T("RtsEnable") @bind-Value=@context.RtsEnable
                             Clearable Style="max-width:300px" Disabled=!isSerial
                             MenuProps="@(props => { props.Auto = true; props.OffsetY = true; })"
                             ItemText=@((u) =>u.ToString())
                             ItemValue=@(u =>u)
                             Dense HideDetails=@("auto")
                             Outlined></MSelect>
                </MCol>





            </MRow>



        </MContainer>
    </MCard>
    ;
        return renderFragment;
    }

}
